package com.zc.controller.newController;

import com.alibaba.fastjson.JSON;
import com.zc.dto.ResultDto;
import com.zc.entity.Role;
import com.zc.entity.Users;
import com.zc.service.permissionService.PermissionService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/permission")
public class PermissionController extends BaseController {
    @Resource
    private PermissionService permissionService;


    /**
     * @param role
     * @param permissionList 权限集合 形式类似"1,2,3,4"
     * @return
     */
    @RequestMapping(value = "/CreateRole", method = RequestMethod.POST)
    public ResultDto CreateRole(Role role, String permissionList, String confirmMID) {
        ResultDto result = new ResultDto();
        try {
            String userInfoString = jedis.get(confirmMID);
            Users users = JSON.parseObject(userInfoString, Users.class);
            permissionService.saveRole(role, permissionList, users);
            result.setResultData(true, 0, "创建角色成功", null);
        } catch (Exception e) {
            LOG.error("CreateRole ERROR :: ,{}", e);
            result.setResultData(false, 1, "创建角色失败", null);
        }
        return result;
    }


    /**
     * 查询该用户下的所有角色
     *
     * @param confirmMID 前台页面保存的当前登陆的用户的标识码(前台登陆生成唯一确定)
     * @return
     */
    @RequestMapping(value = "/findAllRole", method = RequestMethod.POST)
    public ResultDto findAllRole(String confirmMID) {
        ResultDto result = new ResultDto();
        try {
            String userinfoJson = jedis.get(confirmMID);
            Users user = JSON.parseObject(userinfoJson, Users.class);

            String roleId = user.getRoleId();
            List<Role> roles;
            roles = permissionService.findAllRole(roleId);
            result.setResultData(true, 0, "拉取角色信息成功", null);
            result.setData(roles);
        } catch (Exception e) {
            LOG.error("findAllRole error::,{}", e);
            result.setResultData(false, 1, "拉取角色信息失败", null);
        }

        return result;
    }

       /**根据角色id查询相关角色拥有的路径
     * @param confirmMID 身份标识符
     * @return
     */
    @RequestMapping(value = "/findAllPathByroleId", method = RequestMethod.POST)
    public ResultDto findAllPathByroleId(String confirmMID) {
        ResultDto result = new ResultDto();
        try {
            //  根据用户身份标识从redis中获取user信息
            String userinfoJson = jedis.get(confirmMID);
            Users user = JSON.parseObject(userinfoJson, Users.class);
            String roleId = user.getRoleId();
            //拼接好的前台树状图代码
            String tree = permissionService.findAllPathByroleId(roleId);
            result.setResultData(true, 0, "获取path信息数据成功", null);
            result.setData(tree);
        } catch (Exception e) {
            result.setResultData(false, 1, "获取path信息失败", null);
            LOG.error("findAllPathByroleId error ::,{}", e);
        }
        return result;
    }


    /**
     * 根据角色id查询role的相关信息
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/roleDetail", method = RequestMethod.POST)
    public ResultDto roleDetail(Integer roleId) {
        ResultDto result = new ResultDto();
        try {
            Map<String, Object> roleDetail = permissionService.findRoleDetail(roleId);
            result.setData(roleDetail);
            result.setResultData(true, 0, "拉取角色详细信息成功", null);
        } catch (Exception e) {
            LOG.error("roleDetail error ::,{}", e);
            result.setResultData(false, 1, "拉取角色详细信息失败", null);
        }
        return result;
    }

    /**
     * 根据角色id删除对应角色(假删)
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/deleteRoleById", method = RequestMethod.POST)
        public ResultDto deleteRoleById(Integer roleId) {
        ResultDto result = new ResultDto();
        try {
            permissionService.deleteRoleById(roleId);
            result.setResultData(true, 0, "删除角色成功", null);
        } catch (Exception e) {
            LOG.error("deleteRoleById error ::,{}", e);
            result.setResultData(false, 1, "删除角色失败", null);
        }
        return result;
    }

    /**
     * 更新有改动的角色详情
     * @param role
     * @param permissionList
     * @param confirmMID
     * @return
     */
    @RequestMapping(value="/updateRoleDetail" , method = RequestMethod.POST)
    public ResultDto updateRoleDetail(Role role, String permissionList, String confirmMID){
        ResultDto result = new ResultDto();
        try {
            //获得父类的相关信息
            String userInfo = jedis.get(confirmMID);
            Users users = JSON.parseObject(userInfo, Users.class);

            //先删除之前的相关信息
            permissionService.deleteRoleById(role.getId());
            //创建新的角色
            permissionService.saveRole(role,permissionList,users);
            result.setResultData(true, 0, "更新角色成功", null);
        } catch (Exception e) {
            LOG.error("updateRoleDetail error ::,{}", e);
            result.setResultData(false, 1, "更新角色失败", null);
        }
        return result;
    }
}
